// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import ComponentBase from "../../general/Script/component/ComponentBase";
import { AssetsMgr, g_bundleName } from "../../general/Script/mgr/assetsManager";
import DataMgr from "../../general/Script/mgr/dataManager";
import GameMgr from "../../general/Script/mgr/gameManager";
import NativeMgr from "../../general/Script/mgr/nativeManager";
import NetMgr from "../../general/Script/mgr/netManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends ComponentBase {

    login: cc.Node = null;

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        super.onLoad();
        NetMgr.Ins().getSubGameConfig();
        //启动广告
        NativeMgr.Ins().showOpenAds();

        cc.systemEvent.on('openAds', (data) => {
            this.login.active = true;
        }, this);

        this.scheduleOnce(() => {
            this.login.active = true;
        }, 5);

    }

    start() {

    }


    async loginCallBack() {
        console.log('loginCallBack');
        const appid = NativeMgr.Ins().appId();
        const deviceid = NativeMgr.Ins().deviceId();
        GameMgr.Ins().block();
        const infoTotal = await NetMgr.Ins().servo.get(`/auth/device/${appid}/${deviceid}`);
        GameMgr.Ins().unblock();
        if (infoTotal.status == 200) {
            const data = infoTotal.body;
            const token = data.token;
            DataMgr.Ins().token = token;
            NetMgr.Ins().headers.authentication = token;
            NativeMgr.Ins().showBottomBannerAds();
            cc.director.loadScene('home');
        } else {
            GameMgr.Ins().showMsg('deviceLogin', infoTotal.body);
        }




    }

    onDestroy(): void {
        super.onDestroy();
        AssetsMgr.obj().releaseBundle(g_bundleName.login);
        cc.systemEvent.targetOff(this);
    }
}
